﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using System.Data;

namespace SmallERP.Bll.Com
{
    /// <summary>
    /// 关于用户的通用操作类
    /// </summary>
    public class UserBLL
    {
        /// <summary>
        /// 得到所有的用户列表
        /// </summary>
        /// <returns></returns>
        public static List<User_Info> GetAllUsers()
        {
            List<User_Info> listU = new List<User_Info>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    foreach (var u in db.Users.ToList())
            //    {
            //        listU.Add(new User_Info()
            //        {
            //            UserId = u.UserId,
            //            UserName = u.UserName
            //        });
            //    }
            //}
            string QueryUser = "select * from Users";
            DataTable table = DAL.AccessHelper.dataTable(QueryUser);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                User_Info user = new User_Info();
                user.UserId = Convert.ToInt32(table.Rows[i]["UserId"]);
                user.UserName = table.Rows[i]["UserName"].ToString();
                listU.Add(user);
            }
            return listU;
        }
        static string ConvertToin(List<string> inList)
        {
            string rtnStr = "";
            foreach (string str in inList)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    rtnStr += ",";
                }

            }
            return rtnStr.Trim(',');
        }
        public static List<InnerReturn_Info> GetInnerReturnResult(DateTime deStart, DateTime deEnd, List<string> listCreateUser, List<string> listResponseUser)
        {
            List<InnerReturn_Info> listResult = new List<InnerReturn_Info>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    List<InnerReturn> list = db.InnerReturn.Where(i => i.CreateTime <= deEnd && i.CreateTime >= deStart).ToList();
            //    if (listCreateUser.Count > 0)
            //    {
            //        list = list.Where(i => listCreateUser.Contains(i.CreateMan)).ToList();
            //    }
            //    if (listResponseUser.Count > 0)
            //    {
            //        list = list.Where(i => listResponseUser.Contains(i.ResponMan)).ToList();
            //    }

            //    foreach (var i in list)Crea
            //    {
            //        listResult.Add(new InnerReturn_Info()
            //        {
            //            Area = (float)i.PhotoAreas,
            //            CreateTime = i.CreateTime,
            //            CreateUserName = i.CreateMan,
            //            Height = (float)i.Heigth,
            //            Width = (float)i.Width,
            //            OrderProductId = i.OrderproductID,
            //            PhotoId
            //            = i.PhotoId,
            //            PicPath = i.PicPath,
            //            Remark = i.Remark,
            //            ResponseUserName = i.ResponMan
            //        });
            //    }
            //}
            string QueryInnerReturn = "select * from InnerReturn where CreateTime<#" + deEnd + "# and CreateTime>#" + deStart + "# ";
            string CreatrCondition = ConvertToin(listCreateUser);
            if (!string.IsNullOrEmpty(CreatrCondition))
            {
                QueryInnerReturn += " and CreateMan in (" + CreatrCondition + ")";
            }
            string ResponseCondition = ConvertToin(listCreateUser);
            if (!string.IsNullOrEmpty(ResponseCondition))
            {
                QueryInnerReturn += " and CreateMan in (" + ResponseCondition + ")";

            }
            DataTable QTable = DAL.AccessHelper.dataTable(QueryInnerReturn);
            for (int i = 0; i < QTable.Rows.Count; i++)
            {
                InnerReturn_Info info = new InnerReturn_Info();
                info.Area = (float)Convert.ToDecimal(QTable.Rows[i]["PhotoAreas"]);
                info.CreateTime = Convert.ToDateTime(QTable.Rows[i]["CreateTime"]);
                info.CreateUserName = Convert.ToString(QTable.Rows[i]["CreateMan"]);
                info.Height = (float)Convert.ToDecimal(QTable.Rows[i]["Heigth"]);
                info.Width = (float)Convert.ToDecimal(QTable.Rows[i]["Width"]);
                info.OrderProductId = Convert.ToInt32(QTable.Rows[i]["OrderProductId"]);
                info.PhotoId = Convert.ToInt32(QTable.Rows[i]["PhotoId"]);
                info.PicPath = Convert.ToString(QTable.Rows[i]["PicPath"]);
                info.Remark = Convert.ToString(QTable.Rows[i]["Remark"]);
                info.ResponseUserName = Convert.ToString(QTable.Rows[i]["ResponMan"]);

                listResult.Add(info);
            }

                return listResult;
        }
    }
}
